//
//  RecommendFriendsController.m
//  LianShangBao
//
//  Created by 主事丫环HU on 2020/9/8.
//  Copyright © 2020 主事丫环. All rights reserved.
//

#import "RecommendFriendsController.h"
#import "ShareView.h"
#import <JSHAREService.h>
@interface RecommendFriendsController ()
@property (weak, nonatomic) IBOutlet UIButton *shareButton;
@property (weak, nonatomic) IBOutlet UILabel *inviteCodeLabel;

@end

@implementation RecommendFriendsController

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:NO animated:animated];
}
- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:YES animated:animated];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupUI];
    [self requestMyInviteCodeData];
    [Center addObserver:self selector:@selector(applicationWillEnterForeground) name:@"applicationWillEnterForeground" object:nil];
}
- (void)applicationWillEnterForeground{
    self.shareButton.hidden = NO;
}
- (void)setupUI{
    self.title = @"推荐给好友";
    [self.leftbutton setImage:imgname(@"lefticon") forState:(UIControlStateNormal)];
     self.view.backgroundColor = [UIColor colorWithHexString:@"#F5F5F5"];
}
- (void)requestMyInviteCodeData{
    NSMutableDictionary *param = [NSMutableDictionary dictionary];
    param[@"userId"] = USER_ID;
    [NetWorkTool POST:getUser param:param success:^(id dic) {
        self.inviteCodeLabel.text = dic[@"data"][@"invitationCode"];
    } other:^(id dic) {
        
    } fail:^(NSError *error) {
        
    } needUser:YES];
}
- (IBAction)shareButtonClick:(UIButton *)sender {
    self.navigationController.navigationBar.hidden = YES;
    
    NSMutableArray *titlearr = [NSMutableArray arrayWithCapacity:5];
    NSMutableArray *imageArr = [NSMutableArray arrayWithCapacity:5];
    [titlearr addObjectsFromArray:@[@"微信", @"微信朋友圈"]];
    [imageArr addObjectsFromArray:@[@"weixin22",@"weixin11"]];
    
    ShareView *shareView = [[ShareView alloc] initWithShareHeadOprationWith:titlearr andImageArry:imageArr andProTitle:@"分享到"];
    
    [shareView setBtnClick:^(NSInteger btnTag) {
        self.shareButton.hidden = YES;
        [self shareWitipe:btnTag];
    }];
    shareView.cancelButtonClick = ^{
        self.navigationController.navigationBar.hidden = NO;
        self.shareButton.hidden = NO;
    };
    [[UIApplication sharedApplication].keyWindow addSubview:shareView];
}
-(void)shareWitipe:(NSInteger )ind{
    ind++;
    JSHAREMessage *message = [JSHAREMessage message];
    UIImage *image = [self convertViewToImage:self.view];
     NSData *imagedata = UIImagePNGRepresentation(image);

     message.mediaType = JSHAREImage;
    // message.text = [NSString stringWithFormat:@"时间:%@ JShare SDK支持主流社交平台、帮助开发者轻松实现社会化功能！",[self localizedStringTime]];
     message.platform = ind;
     message.image = imagedata;
     //message.images = @[imageData,imageData];
     
     [JSHAREService share:message handler:^(JSHAREState state, NSError *error) {
         if (!error) {
           //  ShowToastWithText(@"分享成功");
         } else {
             ShowToastWithText(@"分享失败");
         }
         self.navigationController.navigationBar.hidden = NO;
         self.shareButton.hidden = NO;
     }];
}
- (UIImage *)convertViewToImage:(UIView *)view {
    
    UIImage *imageRet = [[UIImage alloc]init];
    //UIGraphicsBeginImageContextWithOptions(区域大小, 是否是非透明的, 屏幕密度);
    UIGraphicsBeginImageContextWithOptions(view.frame.size, YES, [UIScreen mainScreen].scale);
    [view.layer renderInContext:UIGraphicsGetCurrentContext()];
    imageRet = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    
    return imageRet;
    
}
//- (NSString *)localizedStringTime{
//    NSDateFormatter*formatter = [[NSDateFormatter alloc]init];[formatter setDateFormat:@"yyy-MM-dd HH:mm:ss"];
//    NSString*dateTime = [formatter stringFromDate:[NSDate date]];
//    return dateTime;
//}
@end
